var express = require('express');
var router = express.Router();
var fs = require("fs");
/* GET home page. */
router.get('/', function(req, res, next) {
	let Query={
		title:req.query.title||'',
		content:req.query.content||'',
		time:req.query.time||''
	};
	// 读取json
	fs.readFile('public/javascripts/data.json',function(err , data){
		obj = JSON.parse(data.toString());
		obj.data=[...obj.data,Query];
		push_in_file(obj);
		res.send(obj)
	})
	
	const push_in_file = obj => {
		fs.writeFile('public/javascripts/data.json',JSON.stringify(obj),function(err){
			console.log('写入成功')
		})
	}
});

//  删除 某个数组

router.get('/delete', function(req, res, next) {
	let number=req.query.number;
	// 读取json
	fs.readFile('public/javascripts/data.json',function(err , data){
		obj = JSON.parse(data.toString());
		obj.data;
		push_in_file(obj);
		res.send(obj)
	})
	
	const push_in_file = obj => {
		fs.writeFile('public/javascripts/data.json',JSON.stringify(obj),function(err){
			console.log('写入成功')
		})
	}
});


module.exports = router;
